cmake_minimum_required(VERSION 3.10)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

project(LANGUAGES CXX)

# 忽略代码中的中文注释引起的MSVC编译器警告
if(MSVC)
    add_compile_options(/source-charset:utf-8)
    add_compile_options(/execution-charset:utf-8)
endif()

if (MSVC)
  set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$<CONFIG:Debug>:Debug>DLL")
endif()


# 创建收集所有源代码文件的函数
function(CollectSourceFiles current_dir variable)
  list(FIND ARGN "${current_dir}" IS_EXCLUDED)
  if(IS_EXCLUDED EQUAL -1)
    file(GLOB COLLECTED_SOURCES
      ${current_dir}/*.cpp
      ${current_dir}/*.h)
    list(APPEND ${variable} ${COLLECTED_SOURCES})

    file(GLOB SUB_DIRECTORIES ${current_dir}/*)
    foreach(SUB_DIRECTORY ${SUB_DIRECTORIES})
      if(IS_DIRECTORY ${SUB_DIRECTORY})
        CollectSourceFiles("${SUB_DIRECTORY}" "${variable}" "${ARGN}")
      endif()
    endforeach()
    set(${variable} ${${variable}} PARENT_SCOPE)
  endif()
endfunction()

if(MSVC)
    set(THIRD_PARTY_PATH ${CMAKE_SOURCE_DIR}/third_party)
elseif(UNIX AND NOT APPLE)
    set(THIRD_PARTY_PATH ${CMAKE_SOURCE_DIR}/third_party_linux)
endif()

# 创建收集所有源代码文件的函数
function(CopyFiles target_name source_dirs file_pattern target_dir variable)
    set(MATCH_FILES)
    foreach(dir IN LISTS source_dirs)
        file(GLOB TEMP_FILES "${dir}/${file_pattern}")
        list(APPEND MATCH_FILES ${TEMP_FILES})
    endforeach()
    add_custom_command(
        TARGET ${target_name} POST_BUILD
        COMMAND ${CMAKE_COMMAND} -E make_directory "${target_dir}" # 确保目标目录存在
        COMMAND ${CMAKE_COMMAND} -E copy_if_different ${MATCH_FILES} "${target_dir}" # 复制文件
    )
endfunction()

# 加入 cmake 库文件搜索范围
list(APPEND CMAKE_PREFIX_PATH 
  ${THIRD_PARTY_PATH}/CdGLLib 
  ${THIRD_PARTY_PATH}/EcdisLib
)
if(UNIX AND NOT APPLE)
  list(APPEND CMAKE_PREFIX_PATH 
    "/usr/lib/x86_64-linux-gnu"
  )
endif()  

#set(BUILD_TARGET "example00" CACHE STRING "Select the build target: 'demo', 'buildsdk', or 'example01'")
set(BUILD_TARGET "buildsdk" CACHE STRING "Select the build target: 'demo', 'buildsdk', or 'example01'")
#set(BUILD_TARGET "demo" CACHE STRING "Select the build target: 'demo', 'buildsdk', or 'example01'")
message(STATUS "BUILD_TARGET is ${BUILD_TARGET}, CMAKE_BUILD_TYPE is ${CMAKE_BUILD_TYPE}")

if(BUILD_TARGET STREQUAL "demo")
    # 调试（静态类）
    add_subdirectory(src/utils)
    add_subdirectory(src/lib)
    add_subdirectory(demo)
    
    message(STATUS "Building demo (development version, debug static link)")
elseif(BUILD_TARGET STREQUAL "demo2")
    add_subdirectory(src/utils)
    add_subdirectory(src/lib)
    add_subdirectory(demo2)
    message(STATUS "Building demo2 (development version, debug static link)")
elseif(BUILD_TARGET STREQUAL "buildsdk")
    set(CMAKE_POSITION_INDEPENDENT_CODE ON)
    # 编译输出动态库
    add_subdirectory(src/utils)
    add_subdirectory(src/lib)
    message(STATUS "Building SDK library")
elseif(BUILD_TARGET MATCHES "^example.*")
    #第三方库
    #add_subdirectory(3rdparty)
    # 示例-引用动态库
    add_subdirectory(examples/${BUILD_TARGET})
    message(STATUS "Building example (for third party use)")
else()
    message(FATAL_ERROR "Invalid BUILD_TARGET value. Please choose 'demo', 'buildsdk', or 'example01'.")
endif()